package com.btex.app;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.btex.app.myView.SwipeBackLayout;
import com.btex.app.tools.SharedPreferencesUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.Locale;

/**
 * Created by wcx on 16/3/16.
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected SwipeBackLayout layout;
    protected TextView tv_title;
    protected TextView tv_menu;
    protected ImageView iv_back;
    protected Context cxt;
    protected String token;
    protected SharedPreferencesUtil sp;

    private Resources mResources;
    private Configuration mConfiguration;
    private DisplayMetrics mDisplayMetrics;
    private boolean mIsEngilsh;
    private String mCity_new;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //统一右滑功能
//        layout = (SwipeBackLayout) LayoutInflater.from(this).inflate(
//                R.layout.base, null);
//        layout.attachToActivity(this);
        initView();
        sp = SharedPreferencesUtil.getSharedPreferences(this);

        mResources = getResources();
        mConfiguration = mResources.getConfiguration();
        mDisplayMetrics = mResources.getDisplayMetrics();

        mIsEngilsh = sp.getBoolean("isEngilsh", false);
        mCity_new = sp.getString("city_new", "0");


        setCity();

//
//        if (!mIsEngilsh) {
//
//            mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//        } else {
//
////            mConfiguration.locale = Locale.US;
//            mConfiguration.locale = Locale.JAPAN;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//        }


        token = sp.getString(Config.TOKEN, null);

        cxt = this;


    }

    private void setCity() {
        mCity_new = sp.getString("city_new", "0");
        if (mCity_new.equals("0")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.SIMPLIFIED_CHINESE);
            } else {
                mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);


        } else if (mCity_new.equals("1")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.US);
            } else {
                mConfiguration.locale = Locale.US;
            }

            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("2")) {

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.JAPANESE);
            } else {
                mConfiguration.locale = Locale.JAPAN;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);


        } else {

            Locale spanish = new Locale("es", "rES");

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(spanish);
            } else {
                mConfiguration.locale = spanish;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
        }
    }

    private void initView() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().getDecorView().setFitsSystemWindows(true);
        }
    }

    public void initData() {
        initTitle();
        setData();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 统一初始化标题
     */
    private void initTitle() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_menu = (TextView) findViewById(R.id.tv_menu);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_menu.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.base_slide_right_in,
                R.anim.base_slide_remain);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.base_slide_right_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);


//        setCity();


    }

    @Override
    protected void onPause() {
        super.onStop();
        MobclickAgent.onPause(this);
    }

    public abstract void setData();


}
